package com.sinoglobal.wallet.activity;

import java.util.ArrayList;
import com.sinoglobal.wallet.R;
import com.sinoglobal.wallet.adapter.W_MyBankAdapter;
import com.sinoglobal.wallet.api.RemoteImpl;
import com.sinoglobal.wallet.app.SinoConstans;
import com.sinoglobal.wallet.entity.W_MyBankVo;
import com.sinoglobal.wallet.entity.W_RsListVo;
import com.sinoglobal.wallet.task.MyAsyncTask;
import com.sinoglobal.wallet.util.NetWorkUtil;
import com.sinoglobal.wallet.view.ListView_ScrollView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 
 * @author lijingjing
 * @createdate 2015-10-21 下午6:20:57
 * @Description: TODO(我的银行卡)
 */
public class W_MyBankActivity extends W_SinoBaseActivity implements OnItemClickListener {
	private LinearLayout mineAddBack_layout;
	private ListView_ScrollView baldetail_listview;
	private W_MyBankAdapter adapter;
	private ArrayList<W_MyBankVo> list = new  ArrayList<W_MyBankVo>();
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.wallet_minebank_activity);
		init();
	}
	private void init(){
		mTemplateTitleText.setText("我的银行卡");
		baldetail_listview = (ListView_ScrollView) findViewById(R.id.order_listView);
		adapter=new W_MyBankAdapter(this);
		baldetail_listview.setAdapter(adapter);
		baldetail_listview.setOnItemClickListener(this);
		
		mineAddBack_layout = (LinearLayout) findViewById(R.id.mineAddBack_layout);
		mineAddBack_layout.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == R.id.mineAddBack_layout){
			goIntent(W_MyAddbankActivity.class);
		}else if (id == R.id.wallet_noData) {
			 if(NetWorkUtil.getNetWorkInfoType(getApplicationContext())==NetWorkUtil.NONE){
					showToast("请检查网络是否连接！");	
					 loadError(true);
					}else{
						loadError(false);;
					}
		}
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		String bankId = list.get(arg2).getUserbcid();
		String bankName = list.get(arg2).getBankName();
		String bankIcon = list.get(arg2).getBankIcon();
		String cardSn = list.get(arg2).getCardSn();
		String cardName = list.get(arg2).getCardName();
		Bundle bankBundle = new Bundle();
		bankBundle.putString("bankId", bankId);
		bankBundle.putString("bankName", bankName);
		bankBundle.putString("bankIcon", SinoConstans.IMAGE_URL + bankIcon);
		bankBundle.putString("cardSn", cardSn);
		bankBundle.putString("cardName", cardName);
		goIntent(W_MyBankXQActivity.class, bankBundle);
	}
	
	//获取银行卡列表
	private void getBankList(){
		MyAsyncTask<W_RsListVo> task = new MyAsyncTask<W_RsListVo>(true,this,true) {

			@Override
			public W_RsListVo before(Void... params) throws Exception {
				// TODO 测试接口
				return RemoteImpl.getInstance().getBankList(W_MyBankActivity.this);
			}

			@Override
			public void after(W_RsListVo result) {

				if (result.getCode().equals("100")) {
					if(result != null){
						list.clear();
						list.addAll(result.getRs());
						sendBroadcast(list.size());
						adapter.setListData(list);
					}
				} else {
					showToast(result.getMsg());
				}
			
			}

			@Override
			public void exception() {
			}
		};
		task.execute();
	}
	@Override
	protected void onResume() {
		super.onResume();
		getBankList();
	}
	private void sendBroadcast(int str) {
		Intent intent = new Intent();
		intent.setAction(SinoConstans.KEY_BalanceReceiver);
		intent.putExtra("bankCardNum", str+"");
		sendBroadcast(intent);
	}
}
